package com.itheima.controller;


import com.itheima.dto.MessageConstant;
import com.itheima.dto.PageResult;
import com.itheima.dto.QueryPageBean;
import com.itheima.dto.Result;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/permission")
public class PermissionController {


    //查找服务
    @Autowired
    private PermissionService permissionService;

    //分页查询
    @RequestMapping("/pageQuery")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = permissionService.pageQuery(queryPageBean);
        return pageResult;
    }


    //添加一个权限
    @RequestMapping("/add")
    public Result add(@RequestBody Permission permission) {
        permissionService.add(permission);
        return Result.success(MessageConstant.ADD_PERMISSION_SUCCESS);
    }

    //编辑权限
    /*用于数据的回显*/
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        Permission data = permissionService.findById(id);
        return  Result.success(data, MessageConstant.QUERY_PERMISSION_SUCCESS);


    }

    /*编辑权限*/
    @RequestMapping("/edit")
    public Result findById(@RequestBody Permission permission) {
        permissionService.edit(permission);
        return Result.success(MessageConstant.EDIT_PERMISSION_SUCCESS);

    }

    //删除一个权限
    @RequestMapping("/delete")
    public Result delete(Integer id) {
        permissionService.deleteById(id);
        return Result.success(MessageConstant.DELETE_PERMISSION_SUCCESS);

    }

    //用于角色的新增或者编辑,查询出所有的权限,展示再表单中
    @RequestMapping("/findAll")
    public Result findAll() {
        List<Permission> data =permissionService.findAll();
        return Result.success(data, MessageConstant.QUERY_PERMISSION_SUCCESS);
    }


}
